package com.example.controller;

import com.example.common.Result;
import com.example.entity.Admin;
import com.example.entity.ZyzAdmin;
import com.example.service.ZyzAdminService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 志愿者管理员控制器
 */


@RestController
@RequestMapping("/api/zyzAdmin")
public class ZyzAdminController {
    @Autowired
    private ZyzAdminService zyzAdminService;

    /**
     * 新增
     */
    @PostMapping("/add")
    public Result add(@RequestBody ZyzAdmin admin) {
        int i = zyzAdminService.add(admin);
        if (i >= 1){
            return Result.success("添加成功");
        }else if (i == -1){
            return Result.error("500","用户名已存在!");
        }else{
            return Result.error("500","添加失败");
        }
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Integer id) {
        int i = zyzAdminService.deleteById(id);
        if (i >= 1){
            return Result.success("删除成功");
        }else{
            return Result.error("500","删除失败");
        }
    }
    /**
     * 批量删除
     */
    @PostMapping("/delete/batch")
    public Result deleteBatch(String ids) {
        int i = zyzAdminService.deleteBatch(ids);
        return Result.success();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public Result updateById(@RequestBody ZyzAdmin admin) {
        zyzAdminService.updateById(admin);
        return Result.success();
    }

    /**
     * 根据id查询
     */
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        ZyzAdmin admin = zyzAdminService.selectById(id);
        return Result.success(admin);
    }

    /**
     * 查询所有
     */
    @GetMapping("/selectAll")
    public Result selectAll(ZyzAdmin admin) {
        List<ZyzAdmin> list = zyzAdminService.selectAll(admin);
        return Result.success(list);
    }

    /**
     * 分页查询
     */
    @GetMapping("/selectPage")
    public Result selectPage(ZyzAdmin admin,
                             @RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<ZyzAdmin> page = zyzAdminService.selectPage(admin, pageNum, pageSize);
        return Result.success(page);
    }

}
